home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Almathera Ten Pack 3: CDPD 3
/
Almathera Ten on Ten - Disc 3: CDPD3.iso
/
ab20
/
ab20_archive
/
archivers
/
uujoin-1.01.lzh
/
FileScan.c
next >
Wrap
C/C++ Source or Header
|
1990-03-12
|
5KB
|
184 lines
#ifdef AZTEC_C
/* Build an expanded list of filenames from a filename argument list. */
#include <stdlib.h>
#include <fcntl.h>
#include <ctype.h>
#include <string.h>
/* The ListInfo structure tracks our filename list building process. */
typedef struct ListInfo {
char **theList;
int slotCount; /* number of slots in the list. */
int entryCount; /* number of slots filled */
} ListInfo;
/* FUNCTION
* AddFile - add a filename to the filename list.
*
* SYNOPSIS
* static int AddFile(ListInfo *fileList, const char *theFile);
*
* DESCRIPTION
* AddFile attempts to add <theFile> to the filename list. The list will be
* expanded if necessary.
*
* AddFile returns zero if successful or 1 on error.
*/
static int
AddFile(ListInfo *fileList, char *theFile)
{
char *fName; /* for a copy of theFile */
char **newList;
int newCount;
size_t newSize;
if (fileList->entryCount >= fileList->slotCount) { /* Expand the list? */
newCount = fileList->slotCount + 50;
/* Note that malloc is used rather than realloc, since I think realloc is
* broken.
*/
newSize = sizeof(char *) * newCount;
newList = malloc(newSize);
if (newList == NULL) return 1; /* Error! */
fileList->slotCount = newCount;
if (fileList->theList) {
/* Copy old stuff to new list. */
memcpy(newList, fileList->theList, newSize);
/* Dispose of the old list. */
free(fileList->theList);
}
fileList->theList = newList;
}
fName = malloc(strlen(theFile)+1);
if (! fName) return 1;
strcpy(fName, theFile);
fileList->theList[fileList->entryCount++] = fName;
return 0;
}
/* FUNCTION
* TestWild - test a file specification for wildcard content.
*
* SYNOPSIS
* static int TestWild(const char *fileName);
*
* DESCRIPTION
* TestWild scans <fileName>, looking for wildcard characters (?, *). If found,
* TestWild returns 1. If not found, TestWild returns 0.
*
* TestWild is provided to prevent unnecessary calls to scdir().
*/
static int
TestWild(char *fileName)
{
char *p, c;
for (p = fileName; c = *p++;)
if (c == '?' || c == '*') return 1;
return 0;
}
/* Note: Lattice users can replace this with stricmp, but this is small enough to
* just leave it in here.
*/
/* FUNCTION
scmpi - perform a case-insensitive string compare.
SYNOPSIS
int scmpi(const char *s1, const char *s2);
DESCRIPTION
Strings <s1> and <s2> are compared, ignoring differences in case.
A result code is returned according to the following:
0 => strings match
<0 => s1 < s2
>0 => s1 > s2
*/
static int
scmpi(const char *s1, const char *s2)
{
int c1, c2, cd;
do {
c1 = tolower(*s1++);
c2 = tolower(*s2++);
if (cd = (c1 - c2)) break;
} while (c1 && c2);
return cd;
}
/* FUNCTION
* Compare - perform case-insensitive compare of two filename strings.
*
* SYNOPSIS
* static int Compare(const void *a, const void *b);
*
* DESCRIPTION
* Compare simply provides an interface between qsort() and a string
* comparison routine. It returns the result of the string comparison.
*/
static int
Compare(const void *a, const void *b)
{
return scmpi(*(char **)a, *(char **)b);
}
/* FUNCTION
* FileScan - scan, expand and optionally sort a file specification list.
*
* SYNOPSIS
* char **FileScan(char **rawList, int rawCount, int *newCount;
* int sortFiles);
*
* DESCRIPTION
* FileScan treats each entry in <rawList> as a file specification and
* attempts to expand it into multiple filenames. The number of entries
* in <rawList> is defined by <rawCount>. If successful, FileScan will
* return a pointer to a new list of filenames, passing the number of entries
* via <newCount>. If <sortFiles> is non-zero, the list will be sorted.
*
* If FileScan is unable to perform an allocation, NULL is returned.
*/
char **
FileScan(char **rawList, int rawCount, int *newCount, int sortFiles)
{
ListInfo fileList; /* Stack-based for reentrancy. */
int rawIndex = 0;
char *rawName;
char *theName;
fileList.theList = NULL; /* Initialize the list structure. */
fileList.slotCount = 0;
fileList.entryCount = 0;
while (rawIndex < rawCount) {
rawName = rawList[rawIndex++];
if (TestWild(rawName)) {
while (theName = scdir(rawName)) /* scdir() is not reentrant... */
if (AddFile(&fileList, theName)) return NULL;
}
else
if (AddFile(&fileList, rawName)) return NULL;
}
*newCount = fileList.entryCount;
if (fileList.theList && sortFiles) {
qsort(fileList.theList, fileList.entryCount, sizeof(char *), Compare);
}
return fileList.theList;
}
#else
#include <Not_Implemented_For_Lattice>
#endif